package com.chainyoung.component_tmperson.ui.adapter;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chainyoung.common.constant.CommonStringConstants;
import com.chainyoung.common.imageloader.ImageLoader;
import com.chainyoung.common.imageloader.glide.GlideImageConfig;
import com.chainyoung.common.utils.StringUtil;
import com.chainyoung.common.utils.Utils;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.entity.WalletDebit;
import com.chainyoung.component_tmperson.ui.activity.debit.WalletApplyDebitActivity;
import com.chainyoung.component_tmperson.ui.activity.debit.WalletDebitActivity;
import com.chainyoung.component_tmperson.ui.activity.financing.WalletFinancingRuleActivity;

import java.util.List;

/**
 * 借贷产品列表适配器
 */
public class WalletDebitAdapter extends BaseQuickAdapter<WalletDebit, BaseViewHolder> {


    private ImageLoader imageLoader;

    public WalletDebitAdapter(@Nullable List<WalletDebit> data) {
        super(R.layout.layout_wallet_debit_item, data);
        imageLoader = Utils.getAppComponent().imageLoader();
    }

    @Override
    protected void convert(BaseViewHolder helper, final WalletDebit item) {

        imageLoader.loadImage(mContext,
                GlideImageConfig
                        .builder()
                        .url(item.getImg())
                        .imagerView((ImageView) (helper.getView(R.id.iv_logo)))
                        .build());

        helper.setText(R.id.tv_coin_name, item.getCoin().toUpperCase() + "借贷");
        helper.setText(R.id.tv_day_rate, StringUtil.subZeroAndDot(String.valueOf(item.getRate().setScale(3))) + "%");
        helper.getView(R.id.tv_repayment_remind).setVisibility(TextUtils.isEmpty(item.getNext_repay_date())?View.GONE:View.VISIBLE);
        if (item.getIs_mine() == 1) {
            helper.setText(R.id.tv_repayment_remind, "自动还款提醒:" + item.getNext_repay_date() + "日自动还款" + StringUtil.subZeroAndDot(item.getNext_repay_amount())
                    + item.getCoin().toUpperCase());
        }

        helper.setText(R.id.tv_min_buy_amout, "云端总资产50%为可借贷金额");

        helper.getView(R.id.rl_financing).setBackgroundResource(item.getIs_mine() == 1 ? R.mipmap.ic_already_buy_bg : R.drawable.shape_financing_bg_white);
        helper.getView(R.id.tv_open).setVisibility(item.getIs_mine() == 1 ? View.GONE : View.VISIBLE);



        helper.getView(R.id.rl_financing).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (item.getIs_mine() == 1) {
                    Intent intent = new Intent(mContext, WalletDebitActivity.class);
                    intent.putExtra("walletDebit", item);
                    mContext.startActivity(intent);
                } else {
                    Intent intent = new Intent(mContext, WalletApplyDebitActivity.class);
                    intent.putExtra("walletDebit", item);
                    mContext.startActivity(intent);
                }
            }
        });

        helper.getView(R.id.tv_rules).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(mContext, WalletFinancingRuleActivity.class);
                intent1.putExtra(CommonStringConstants.URL, item.getRule());
                intent1.putExtra("title", "规则");
                mContext.startActivity(intent1);
            }
        });


    }


}


